﻿#from google.appengine.ext import db

#import models
import kb

class Classifier:
    ##################################################################################
    
    ##################################################################################
    def __init__(self):
        pass
    
    def classify(self, message):
        #clss = models.Classification.all()
        #message = 
        return None
    def addToAccount(msg):
        pass
        
    def getMarkedHTML(self, message):
        markedHTML = u""
        annotationList = kb.getAnnotationList(message)
        last_end = 0
        goods_name = u""
        weight = u""
        money = u""
        unit = u""
        
        if annotationList and len(annotationList)>0:
            for annotation in annotationList:
                #print annotation
                (start, end), type, str = annotation
                if type==u"qutity_weight":
                    weight = str
                elif type==u"qutity_money":
                    money = str
                elif type==u"qutity_unit":
                    unit = str
                    
                gap_str = message[last_end:start]
                markedHTML = markedHTML + gap_str
                
                goods_name = getGoodsName(goods_name, gap_str)
                    
                markedHTML = markedHTML + u"<span class='" + type + u"'>" + str + u"</span>"
                last_end = end
            
            gap_str = message[last_end:]
            markedHTML = markedHTML + gap_str
            
            goods_name = getGoodsName(goods_name, gap_str)
            
            #print u"goods_name:" + goods_name + u"|"
            if goods_name!=u"":
                markedHTML = markedHTML.replace(goods_name, u"<span class='goods_name'>" + goods_name + u"</span>")
        else:
            markedHTML = message
        #print goods_name + "," + unit + "," + weight + "," + money
        #print markedHTML
        return goods_name, unit, weight, money, markedHTML

def getGoodsName(goods_name, gap_str):
    #gap_str = gap_str.replace("&nbsp;", "")
    if gap_str and gap_str.strip() != u"":
        #print "gap_str.strip():"+gap_str.strip(u"  ")+"|"
        if len(gap_str.strip()) > len(goods_name):
            goods_name = gap_str.strip()
    return goods_name
    
if __name__ == "__main__":
    classifier = Classifier()
    #classifier.getMarkedHTML(u"毛毛鲅鱼馅速冻水饺 1袋 500克 18.9元")
    #classifier.getMarkedHTML(u"50公斤大米 200元 3千万元 12立方米 大米 一袋 伍拾贰斤 壹佰三十四元五角")
    #classifier.getMarkedHTML(u"一块猪肉三斤半  48元")
    #classifier.getMarkedHTML(u"三斤五花肉")
    classifier.getMarkedHTML(u"特价猪排5斤45.5元")
    